package com.supor.aiot.common.base;

import android.app.Activity;
import android.content.Intent;
import android.nfc.Tag;
import android.provider.Settings;

import com.android.baseconfig.base.BaseActivity;
import com.android.baseconfig.base.BasePresenter;
import com.android.baseconfig.common.entity.parameter.ShowDialogParameter;
import com.android.baseconfig.common.utils.DialogUtils;
import com.android.baseconfig.common.utils.Logc;
import com.android.baseconfig.common.views.AppBasicDialog;
import com.supor.aiot.common.R;
import com.supor.nfc.lib.utils.NfcUtils;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2020, by het, Shenzhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: BaseNfcActivity <br>
 * 作者: lei <br>
 * 日期: 2020/11/20 <br>
 */
public abstract class BaseNfcActivity extends BaseActivity {

    public static final int REQ_NFC_OPEN = 0X9000;

    private Activity activity;

    private int status;

    /**
     * 设备不支持NFC
     */
    public static final int NFC_NOT_SUPPORT = -1;
    /**
     * NFC已关闭
     */
    public static final int NFC_DISABLED = 0;
    /**
     * NFC开启了
     */
    public static final int NFC_ABLED = 1;

    /**
     * nfc tag
     */
    private Tag mTag;

    private AppBasicDialog settingDialog;

    /**
     * 是否需要开启NFC相关对象
     *
     * @return true 需要初始化nfc
     */
    public boolean isOpenNfc() {
        return true;
    }

    @Override
    public void initData() {
        NfcUtils.init(this);
        activity = this;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    public Tag getNfcTag() {
        return mTag;
    }

    public void setNfcTag(Tag mTag) {
        this.mTag = mTag;
    }

    @Override
    public void updateNFCStatus(int state) {
        super.updateNFCStatus(state);
        NfcUtils.init(this);
        NfcUtils.configNfc(this);
        processNFC();
    }

    protected void processNFC() {
        if (NfcUtils.NFC_STATE == 2) {
            showNFCDialog();
        } else {
            closeNFCDialog();
        }
    }

    private void showNFCDialog() {
        if (settingDialog != null) {
            if (settingDialog.isShowing()) {
                return;
            } else {
                settingDialog.show();
            }
            return;
        }
        ShowDialogParameter parameter = new ShowDialogParameter()
                .setTitle(getString(R.string.nfc_close))
                .setMsg(getString(R.string.nfc_setting_tip))
                .setStrYes(getString(R.string.setting))
                .setStrNo(getString(R.string.cancel));
        settingDialog = DialogUtils.opDialog(this, parameter,
                (dialog, which) -> startActivity(new Intent(Settings.ACTION_NFC_SETTINGS)),
                (dialog, which) -> {
                    dialog.cancel();
                    finish();
                });
        settingDialog.show();
    }

    private void closeNFCDialog() {
        if (settingDialog != null && settingDialog.isShowing()) {
            settingDialog.dismiss();
        }
    }

    /**
     * 获取设备NFC状态
     */
    public int getNfcStatus() {
        return (NfcUtils.mNfcAdapter == null) ? NFC_NOT_SUPPORT : NfcUtils.mNfcAdapter.isEnabled() ? NFC_ABLED :
                NFC_DISABLED;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isOpenNfc()) {
            if (NfcUtils.mNfcAdapter != null) {
                NfcUtils.configNfc(this);
                NfcUtils.mNfcAdapter.enableForegroundDispatch(this, NfcUtils.mPendingIntent,
                        null, null);
            }
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Logc.i(TAG, "--------------nfc onNewIntent-------------");
        if (NfcUtils.mNfcAdapter == null) {
            NfcUtils.init(this);
            NfcUtils.configNfc(this);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (isOpenNfc()) {
            if (NfcUtils.mNfcAdapter != null) {
                NfcUtils.mNfcAdapter.disableForegroundDispatch(this);
            }
        }
    }

    /**
     * 设备不支持NFC功能
     */
    public void onNfcNotSupportClick() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Logc.i("requestCode:" + requestCode + ", resultCode:" + resultCode);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        NfcUtils.mNfcAdapter = null;
    }
}
